# -*- coding: utf-8 -*- 
# 开发者: 夏明德           
# 开发时间 : 2022/12/26 11:10
# 文件名称: BMI对比公式.py
import math
import os
import openpyxl


class bmi(object):

    def hight_Bmi_Man(self, age, num):
        """
        接收传递的值，通过年龄来比对体重指数并返回结果！
        """
        if age == 6:
            if num <= 13.2:
                return '中重度消瘦'
            if num < 13.4:
                return '轻度消瘦'
            if num < 16.7:
                return '正常'
            if num < 18.1:
                return '超重'
            if num >= 18.1:
                return '肥胖'
        if age == 7:
            if num <= 13.5:
                return '中重度消瘦'
            if num < 13.9:
                return '轻度消瘦'
            if num < 17.4:
                return '正常'
            if num < 19.2:
                return '超重'
            if num >= 19.2:
                return '肥胖'
        if age == 8:
            if num <= 13.6:
                return '中重度消瘦'
            if num < 14.0:
                return '轻度消瘦'
            if num < 18.1:
                return '正常'
            if num < 20.3:
                return '超重'
            if num >= 20.3:
                return '肥胖'
        if age == 9:
            if num <= 13.7:
                return '中重度消瘦'
            if num < 14.1:
                return '轻度消瘦'
            if num < 18.9:
                return '正常'
            if num < 21.4:
                return '超重'
            if num >= 21.4:
                return '肥胖'
        if age == 10:
            if num <= 13.9:
                return '中重度消瘦'
            if num < 14.4:
                return '轻度消瘦'
            if num < 19.6:
                return '正常'
            if num < 22.5:
                return '超重'
            if num >= 22.5:
                return '肥胖'
        if age == 11:
            if num <= 14.2:
                return '中重度消瘦'
            if num < 14.9:
                return '轻度消瘦'
            if num < 20.3:
                return '正常'
            if num < 23.6:
                return '超重'
            if num >= 23.6:
                return '肥胖'
        if age == 12:
            if num <= 14.4:
                return '中重度消瘦'
            if num < 15.4:
                return '轻度消瘦'
            if num < 21.0:
                return '正常'
            if num < 24.7:
                return '超重'
            if num >= 24.7:
                return '肥胖'
        if age == 13:
            if num <= 14.8:
                return '中重度消瘦'
            if num < 15.9:
                return '轻度消瘦'
            if num < 21.9:
                return '正常'
            if num < 25.7:
                return '超重'
            if num >= 25.7:
                return '肥胖'
        if age == 14:
            if num <= 15.3:
                return '中重度消瘦'
            if num < 16.4:
                return '轻度消瘦'
            if num < 22.6:
                return '正常'
            if num < 26.4:
                return '超重'
            if num >= 26.4:
                return '肥胖'
        if age == 15:
            if num <= 15.8:
                return '中重度消瘦'
            if num < 16.9:
                return '轻度消瘦'
            if num < 23.1:
                return '正常'
            if num < 26.9:
                return '超重'
            if num >= 26.9:
                return '肥胖'
        if age == 16:
            if num <= 16.2:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num < 23.5:
                return '正常'
            if num < 27.4:
                return '超重'
            if num >= 27.4:
                return '肥胖'
        if age == 17:
            if num <= 16.6:
                return '中重度消瘦'
            if num < 17.7:
                return '轻度消瘦'
            if num < 23.8:
                return '正常'
            if num < 27.8:
                return '超重'
            if num >= 27.8:
                return '肥胖'
        if age == 18:
            if num <= 16.8:
                return '中重度消瘦'
            if num < 17.9:
                return '轻度消瘦'
            if num < 24.0:
                return '正常'
            if num < 28.0:
                return '超重'
            if num >= 28.0:
                return '肥胖'
        if age > 18:
            if num <= 16.8:
                return '中重度消瘦'
            if num < 17.9:
                return '轻度消瘦'
            if num >= 17.9:
                return '正常'

    def hight_Bmi_Woman(self, age, num):
        """
        接收传递的值，通过年龄来比对体重指数并返回结果！
        """
        if age == 6:
            if num <= 12.8:
                return '中重度消瘦'
            if num < 13.1:
                return '轻度消瘦'
            if num < 16.5:
                return '正常'
            if num < 18.0:
                return '超重'
            if num >= 18.0:
                return '肥胖'
        if age == 7:
            if num <= 13.0:
                return '中重度消瘦'
            if num < 13.4:
                return '轻度消瘦'
            if num < 17.2:
                return '正常'
            if num < 19.0:
                return '超重'
            if num >= 19.0:
                return '肥胖'
        if age == 8:
            if num <= 13.1:
                return '中重度消瘦'
            if num < 13.6:
                return '轻度消瘦'
            if num < 18.1:
                return '正常'
            if num < 19.9:
                return '超重'
            if num >= 19.9:
                return '肥胖'
        if age == 9:
            if num <= 13.2:
                return '中重度消瘦'
            if num < 13.8:
                return '轻度消瘦'
            if num < 19.0:
                return '正常'
            if num < 21.0:
                return '超重'
            if num >= 21.0:
                return '肥胖'
        if age == 10:
            if num <= 13.3:
                return '中重度消瘦'
            if num < 14.0:
                return '轻度消瘦'
            if num < 20.0:
                return '正常'
            if num < 22.1:
                return '超重'
            if num >= 22.1:
                return '肥胖'
        if age == 11:
            if num <= 13.7:
                return '中重度消瘦'
            if num < 14.3:
                return '轻度消瘦'
            if num < 21.1:
                return '正常'
            if num < 23.3:
                return '超重'
            if num >= 23.3:
                return '肥胖'
        if age == 12:
            if num <= 14.1:
                return '中重度消瘦'
            if num < 14.7:
                return '轻度消瘦'
            if num < 21.9:
                return '正常'
            if num < 24.5:
                return '超重'
            if num >= 24.5:
                return '肥胖'
        if age == 13:
            if num <= 14.6:
                return '中重度消瘦'
            if num < 15.3:
                return '轻度消瘦'
            if num < 22.6:
                return '正常'
            if num < 25.6:
                return '超重'
            if num >= 25.6:
                return '肥胖'
        if age == 14:
            if num <= 15.3:
                return '中重度消瘦'
            if num < 16.0:
                return '轻度消瘦'
            if num < 23:
                return '正常'
            if num < 26.3:
                return '超重'
            if num >= 26.3:
                return '肥胖'
        if age == 15:
            if num <= 16.0:
                return '中重度消瘦'
            if num < 16.6:
                return '轻度消瘦'
            if num < 23.4:
                return '正常'
            if num < 26.9:
                return '超重'
            if num >= 26.9:
                return '肥胖'
        if age == 16:
            if num <= 16.2:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num < 23.7:
                return '正常'
            if num < 27.4:
                return '超重'
            if num >= 27.4:
                return '肥胖'
        if age == 17:
            if num <= 16.4:
                return '中重度消瘦'
            if num < 17.0:
                return '轻度消瘦'
            if num < 23.9:
                return '正常'
            if num < 27.8:
                return '超重'
            if num >= 27.8:
                return '肥胖'
        if age == 18:
            if num <= 16.7:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num < 24.0:
                return '正常'
            if num < 28.0:
                return '超重'
            if num >= 28.0:
                return '肥胖'
        if age > 18:
            if num <= 16.7:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num >= 17.3:
                return '正常'

    def hight_man(self, age, stature):
        """
        接收传递的值，通过年龄来比对身高并返回结果！
        """
        if age == 6:
            if stature <= 1.063:
                return '生长迟缓'
            else:
                return '正常'
        if age == 7:
            if stature <= 1.113:
                return '生长迟缓'
            else:
                return '正常'
        if age == 8:
            if stature <= 1.154:
                return '生长迟缓'
            else:
                return '正常'
        if age == 9:
            if stature <= 1.206:
                return '生长迟缓'
            else:
                return '正常'
        if age == 10:
            if stature <= 1.252:
                return '生长迟缓'
            else:
                return '正常'
        if age == 11:
            if stature <= 1.291:
                return '生长迟缓'
            else:
                return '正常'
        if age == 12:
            if stature <= 1.331:
                return '生长迟缓'
            else:
                return '正常'
        if age == 13:
            if stature <= 1.369:
                return '生长迟缓'
            else:
                return '正常'
        if age == 14:
            if stature <= 1.419:
                return '生长迟缓'
            else:
                return '正常'
        if age == 15:
            if stature <= 1.496:
                return '生长迟缓'
            else:
                return '正常'
        if age == 16:
            if stature <= 1.551:
                return '生长迟缓'
            else:
                return '正常'
        if age == 17:
            if stature <= 1.568:
                return '生长迟缓'
            else:
                return '正常'
        if age == 18:
            if stature <= 1.571:
                return '生长迟缓'
            else:
                return '正常'
        if age > 18:
            if stature <= 1.572:
                return '生长迟缓'
            else:
                return '正常'

    def hight_woman(self, age, stature):
        """
        接收传递的值，通过年龄来比对身高并返回结果！
        """
        if age == 6:
            if stature <= 1.057:
                return '生长迟缓'
            else:
                return '正常'
        if age == 7:
            if stature <= 1.102:
                return '生长迟缓'
            else:
                return '正常'
        if age == 8:
            if stature <= 1.145:
                return '生长迟缓'
            else:
                return '正常'
        if age == 9:
            if stature <= 1.195:
                return '生长迟缓'
            else:
                return '正常'
        if age == 10:
            if stature <= 1.239:
                return '生长迟缓'
            else:
                return '正常'
        if age == 11:
            if stature <= 1.286:
                return '生长迟缓'
            else:
                return '正常'
        if age == 12:
            if stature <= 1.336:
                return '生长迟缓'
            else:
                return '正常'
        if age == 13:
            if stature <= 1.388:
                return '生长迟缓'
            else:
                return '正常'
        if age == 14:
            if stature <= 1.429:
                return '生长迟缓'
            else:
                return '正常'
        if age == 15:
            if stature <= 1.454:
                return '生长迟缓'
            else:
                return '正常'
        if age == 16:
            if stature <= 1.468:
                return '生长迟缓'
            else:
                return '正常'
        if age == 17:
            if stature <= 1.473:
                return '生长迟缓'
            else:
                return '正常'
        if age == 18:
            if stature <= 1.475:
                return '生长迟缓'
            else:
                return '正常'
        if age > 18:
            if stature <= 1.476:
                return '生长迟缓'
            else:
                return '正常'

    def gjeyesight1(self, eye):
        """
        国际视力标准读数！
        """
        if eye < 0.25:
            return '重度视力不良'
        if eye < 0.4:
            return '中度视力不良'
        if eye < 0.9:
            return '轻度视力不良'
        if eye >= 0.9:
            return '正常'

    def zgeyesight2(self, eye):
        """
        中国学生视力标准读数！
        """
        if eye < 4.4:
            return '重度视力不良'
        if eye < 4.6:
            return '中度视力不良'
        if eye < 4.9:
            return '轻度视力不良'
        if eye >= 4.9:
            return '正常'

    def count(self, num):
        """
        统计非空单元格数量
        """
        a = 0
        for i in num[4:]:
            if i.value is None or i.value == '未测':
                pass
            else:
                a += 1
        return a

    def gender(self, num):
        """
        统计男性及女性数量
        """
        man = 0
        women = 0
        for i in num[4:]:
            if i.value == '女':
                women += 1
            if i.value == '男':
                man += 1
        data = {
            'man': man,
            'women': women,
        }
        return data

    def daojiancount(self, num):
        a = 0
        for i in num[2:]:
            if i.value is None or i.value == 0:
                pass
            else:
                a += 1
        return a

    def wirte_f1(self, data):
        """
        以追加的形式生成文件
        """
        if os.path.exists("./report/附件一学生健康体检基本情况汇总.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件一学生健康体检基本情况汇总.xlsx")
            book = workbook['Sheet1']
            book.append([data['banji'], data['students'], data['nodaojians'], data['daojians'], data['jcl']])
            workbook.save("./report/附件一学生健康体检基本情况汇总.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件一学生健康体检基本情况汇总.xlsx")
            book = workbook['Sheet1']
            book.append([data['banji'], data['students'], data['nodaojians'], data['daojians'], data['jcl']])
            workbook.save("./report/附件一学生健康体检基本情况汇总.xlsx")
        return

    def wirte_f2(self, data):
        if os.path.exists("./report/附件二常见疾病及缺点检出情况一.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件二常见疾病及缺点检出情况一.xlsx")
            book = workbook['Sheet1']
            book.append([
                data['banji'],
                data['daojians'],
                data['xzyc'],
                data['xzyc_l'],
                data['fbyc'],
                data['fbyc_l'],
                data['gpyc'],
                data['gpyc_l'],
                data['tbyc'],
                data['tbyc_l'],
                data['jbyc'],
                data['jbyc_l'],
                data['xbyc'],
                data['xbyc_l'],
                data['jzyc'],
                data['jzyc_l'],
                data['szyc'],
                data['szyc_l'],
                data['pfyc'],
                data['pfyc_l'],
                data['lbyc'],
                data['lbyc_l'],
            ])
            workbook.save("./report/附件二常见疾病及缺点检出情况一.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件二常见疾病及缺点检出情况一.xlsx")
            book = workbook['Sheet1']
            book.append([
                data['banji'],
                data['daojians'],
                data['xzyc'],
                data['xzyc_l'],
                data['fbyc'],
                data['fbyc_l'],
                data['gpyc'],
                data['gpyc_l'],
                data['tbyc'],
                data['tbyc_l'],
                data['jbyc'],
                data['jbyc_l'],
                data['xbyc'],
                data['xbyc_l'],
                data['jzyc'],
                data['jzyc_l'],
                data['szyc'],
                data['szyc_l'],
                data['pfyc'],
                data['pfyc_l'],
                data['lbyc'],
                data['lbyc_l'],
            ])
            workbook.save("./report/附件二常见疾病及缺点检出情况一.xlsx")
        return

    def wirte_f3(self, data):
        if os.path.exists("./report/附件三常见疾病及缺点检出情况二.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件三常见疾病及缺点检出情况二.xlsx")
            book = workbook['Sheet1']
            book.append([
                data['banji'],
                data['daojians'],
                data['syyc'],
                data['syyc_l'],
                data['yzyc'],
                data['yzyc_l'],
                data['bbyc'],
                data['bbyc_l'],
                data['bttyc'],
                data['bttyc_l'],
                data['qsl'],
                data['qsl_l'],
                data['zsl'],
                data['zsl_l'],
                data['zdsl'],
                data['zdsl_l'],
                data['eryc'],
                data['eryc_l'],
                data['qcyc'],
                data['qcyc_l'],
])
            workbook.save("./report/附件三常见疾病及缺点检出情况二.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件三常见疾病及缺点检出情况二.xlsx")
            book = workbook['Sheet1']
            book.append([
                data['banji'],
                data['daojians'],
                data['syyc'],
                data['syyc_l'],
                data['yzyc'],
                data['yzyc_l'],
                data['bbyc'],
                data['bbyc_l'],
                data['bttyc'],
                data['bttyc_l'],
                data['qsl'],
                data['qsl_l'],
                data['zsl'],
                data['zsl_l'],
                data['zdsl'],
                data['zdsl_l'],
                data['eryc'],
                data['eryc_l'],
                data['qcyc'],
                data['qcyc_l'],
            ])
            workbook.save("./report/附件三常见疾病及缺点检出情况二.xlsx")
        return

    def wirte_f4(self, data):
        if os.path.exists("./report/附件四学生健康体检数据汇总.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件四学生健康体检数据汇总.xlsx")
            book = workbook['Sheet1']
            book.append([
            data['banji'],
            data['students'],
            data['daojians'],
            data['nodaojians'],
            data['man'],
            data['woman'],
            data['manxs'],
            data['mancz'],
            data['manfp'],
            data['nanxingch'],
            data['womenxs'],
            data['womencz'],
            data['womenfp'],
            data['nvxingch'],
            data['qsl'],
            data['zsl'],
            data['zdsl'],
            data['xzyc'],
            data['fbyc'],
            data['gpyc'],
            data['tbyc'],
            data['jbyc'],
            data['xbyc'],
            data['jzyc'],
            data['szyc'],
            data['pfyc'],
            data['lbyc'],
            data['eryc'],
            data['bbyc'],
            data['bttyc'],
            data['qcyc'],
            data['yzyc'],
            ])
            workbook.save("./report/附件四学生健康体检数据汇总.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件四学生健康体检数据汇总.xlsx")
            book = workbook['Sheet1']
            book.append([
                data['banji'],
                data['students'],
                data['daojians'],
                data['nodaojians'],
                data['man'],
                data['woman'],
                data['manxs'],
                data['mancz'],
                data['manfp'],
                data['nanxingch'],
                data['womenxs'],
                data['womencz'],
                data['womenfp'],
                data['nvxingch'],
                data['qsl'],
                data['zsl'],
                data['zdsl'],
                data['xzyc'],
                data['fbyc'],
                data['gpyc'],
                data['tbyc'],
                data['jbyc'],
                data['xbyc'],
                data['jzyc'],
                data['szyc'],
                data['pfyc'],
                data['lbyc'],
                data['eryc'],
                data['bbyc'],
                data['bttyc'],
                data['qcyc'],
                data['yzyc'],
            ])
            workbook.save("./report/附件四学生健康体检数据汇总.xlsx")
        return

    def shengzhang(self, num1):
        """
        计算营养吸收人数
        """

        # 男性消瘦
        a = 0
        # 男性超重
        b = 0
        # 男性肥胖
        c = 0
        # 女性消瘦
        d = 0
        # 女性超重
        e = 0
        # 女性肥胖
        f = 0
        for x, y in num1:
            if y.value == "轻度消瘦" or y.value == "中重度消瘦":
                if x.value == "男":
                    a += 1
                else:
                    d += 1
            elif y.value == "超重":
                if x.value == "男":
                    b += 1
                else:
                    e += 1
            elif y.value == "肥胖":
                if x.value == "男":
                    c += 1
                else:
                    f += 1
            else:
                pass
        data = {
            '男性消瘦': a,
            '男性超重': b,
            '男性肥胖': c,
            '女性消瘦': d,
            '女性超重': e,
            '女性肥胖': f,
        }
        return data

    def shengaojieguo(self, num):
        """
        分别统计男性和女性生长迟缓的人数
        """
        # 男性生长迟缓
        a = 0
        # 女性生长迟缓
        b = 0
        for x, y in num:
            if x.value is None or x.value == '性别' or y.value is None or y.value == '身高对比结果' or y.value == '正常':
                pass
            else:
                if y.value == "生长迟缓":
                    if x.value == "男":
                        a += 1
                    else:
                        b += 1
        data = {
            'man': a,
            'woman': b,
        }
        print(data['man'], data['woman'])
        return data

    def shilicount(self, shili):
        """
        计算三种视力程度 分别人数
        """
        # 轻度视力不良计数
        a = 0
        # 中度视力不良计数
        b = 0
        # 重度视力不良计数
        c = 0
        for x in shili:
            if x.value == '轻度视力不良':
                a += 1
            elif x.value == '中度视力不良':
                b += 1
            elif x.value == '重度视力不良':
                c += 1
            else:
                pass
            data = {'qsl': a, 'zsl': b, 'zdsl': c}
        return data

    def baifenbi(self, a, b):
        """
        计算百分比 如果被除数为0 则直接返回0
        """
        if a == 0 or b == 0:
            return 0
        else:
            return "{:.2%}".format(float(a) / float(b))

    def Sumhe(self, num):
        """
        列纵向求和
        """
        a = 0
        for i in num:
            if i.value == 0 or type(i.value) == str or i.value is None:
                pass
            else:
                a = a + int(i.value)
        return a

    def BMI_result(self, num1, num2):
        num = num1 / num2 ** 2
        return num

    def BMI_result2(self, kg, lg):
        num = float(kg) / float((float(lg) / 100) ** 2)
        return round(num, 1)

    def xscount(self, data):
        """
        计算营养吸收人数
        """

        # 消瘦
        a = 0
        # 超重
        b = 0
        # 肥胖
        c = 0
        for i in data[4:]:
            if i.value is None or i.value == '未测':
                pass
            else:
                if i.value == "轻度消瘦" or i.value == "中重度消瘦":
                    a += 1
                if i.value == "超重":
                    b += 1
                if i.value == "肥胖":
                    c += 1
        data = {
            '消瘦': a,
            '超重': b,
            '肥胖': c,
        }
        return data

    def chcount(self, data):
        """
        身高对比结果
        """
        # 生长迟缓
        a = 0
        for i in data[4:]:
            if i.value is None or i.value == '未测' or i.value == '正常':
                pass
            else:
                if i.value == "生长迟缓":
                    a += 1
                else:
                    pass
        return a